<?php 
	session_start();
	require_once("../../wp-blog-header.php");
	
	$action = $_GET["action"];
	if($action == "loginout")
	{
		unset($_SESSION["blogtype"]);
		unset($_SESSION["token"]);
		unset($_SESSION["syscate"]);
		unset($_SESSION["usercate"]);
		unset($_SESSION["username"]);
		unset($_SESSION["userid"]);
	}
	
	$token = $_SESSION["token"];
	$username = $_SESSION["username"];
	$userid = $_SESSION["userid"];
	$syscate = $_SESSION["syscate"];
	$usercate = $_SESSION["usercate"];
	
	$blogtype= $_SESSION["blogtype"];
	$posturl = "";
	if($blogtype == "oschina"){
		$posturl = "osc/post_osc.php";
	}else if($blogtype == "csdn"){
		$posturl = "csdn/post_csdn.php";
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo( 'charset' ); ?>" />
<title>博客（wp2Blog）导入工具</title>
<meta name="keywords" content="wordpress,oschina" />
<style type="text/css">
body {font-family: "微软雅黑";font-size: 14px;margin: 0;color: #000000;background: #ffffff;}
li {margin-top: 8px;}
.page {padding: 4px; border-top: 1px #EEEEEE solid}
.author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid}
#nav, #content, #footer,#useinfo,#useraction,#resultinfo {padding: 8px; border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top: 10px;}
li{list-style-type:none;}
</style>
<script type="text/javascript" src="../static/js/jquery.js"></script>
</head>
<body vlink="#333333" link="#333333">
	
	<h2 style="text-align: center; margin-top: 20px">博客（wp2Blog）导入工具 (支持WP导入OSChina博客和CSDN博客)</h2>
<?php
	include("header.php");
	include("content.php");
	include("footer.php");
?>
	
<script>
	function cateck(ck){
		var ischecked = $(ck).attr("checked");
		var catid = $(ck).val();
		if(ischecked){
			$(".ckpost[catid="+catid+"]").attr("checked",true);
		}else{
			$(".ckpost[catid="+catid+"]").attr("checked",false);
		}
	}
	
	function commit(){
		if(commitcheck()){
			
			$("#btnCommit").val("导入中，请稍等。。。");
			$("#btnCommit").attr("disabled","disabled");
			
			var datastring = "[";
			$(".ckcate").each(function(){
				
				var catid = $(this).val();
				var postCheckedObj = $(".ckpost[catid="+catid+"]:checked");
				if(postCheckedObj.size() > 0){
					
					var onecatedata = "{";
					
					var syscateid = $(".syscatelist[catid="+catid+"]").val();
					
					var usercateid = $(".usercatelist[catid="+catid+"]").val();
					//csdn api的分类信息使用文本形式，非id
					var usercatename = $(".usercatelist[catid="+catid+"] option:selected").text();
	
					
					var postids = "[";
					$(postCheckedObj).each(function(){
						postids += ($(this).val()+ ",");
					});
					postids = postids.substring(0,postids.length-1);
					postids += "]";
					
					onecatedata += ('"syscateid":"'+syscateid+'","usercateid":"'+usercateid+'","usercatename":"'+usercatename+'","postids":"'+postids+'"');
					onecatedata += "}";
					
					datastring += (onecatedata +",");
				}
			});
			datastring = datastring.substring(0,datastring.length-1);
			datastring += "]";
			
			$.ajax({
			   type: "POST",
			   url: "<?php echo $posturl;?>",
			   data: "data="+datastring+"&token=<?php echo $token?>"+"&userid=<?php echo $userid?>",
			   success: function(msg){
					var _msg = eval('('+msg+')');
					
					var str = "";
					for(var i=0; i<_msg.length; i++)  
					{  
						str += (_msg[i]+"<br/>");
					}
					$("#result").append(str);
					$("#resultinfo").show();
					
					$(".ckpost").attr("checked",false);
					
					$("#btnCommit").val("开始导入");
					$("#btnCommit").removeAttr("disabled");
			   }
			});
		}
	}
	
	function commitcheck(){
		if("<?php echo $token;?>"==""){
			alert("请先登录");
			return false;
		}
		var checkedCount = $(".ckpost:checked").length;
		if(checkedCount < 1){
			alert("请先选择文章");
			return false;
		}
		return true;
	}
	
	if($(".syscatelist option").size()==0 || $(".usercatelist option").size()==0){
		$(".syscatelist").append("<option>请先登录</option>");
		$(".usercatelist").append("<option>请先登录</option>");
	}
	
	function showinfodetail(){
		$("#infodetail").toggle();
	}
	
</script>


</body>
</html>